package Frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JPanel;

public class FCJtable extends JPanel {
	private HashMap<String, String> list = new HashMap<String, String>();
	private Vector<String> key = new Vector<String>();
	private int select = -1;
	private int firstshow = 0;

	public FCJtable() {
		addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				int ynum = arg0.getY() / 16 + firstshow;
				if (ynum > -1 && ynum < key.size())
					select = (select == ynum) ? -1 : ynum;
				else
					select = -1;
				repaint();
			}
		});
		addMouseWheelListener(new MouseWheelListener() {
			@Override
			public void mouseWheelMoved(MouseWheelEvent e) {
				if (e.getWheelRotation() == 1) {
					if (firstshow < key.size() - 5)
						firstshow++;
				} else {
					if (firstshow > 0)
						firstshow--;
				}
				repaint();
			}
		});
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(new Color(240, 240, 240));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setFont(new Font("黑体", 0, 15));
		for (int i = firstshow; i < key.size(); i++) {
			if (i == select)
				g.setColor(Color.yellow);
			else
				g.setColor(Color.white);
			g.fillRect(0, 16 * (i - firstshow) + 1, getWidth(), 15);
			g.setColor(Color.black);
			g.drawString(key.get(i), 5, 16 * (i - firstshow) + 14);
			g.drawLine(0, 16 * (i - firstshow) + 16, getWidth(), 16 * (i - firstshow) + 16);
		}
		g.setColor(Color.black);
		g.drawLine(0, 0, getWidth(), 0);
		g.drawLine(0, 0, 0, getHeight());
		g.drawLine(getWidth() - 1, getHeight() - 1, getWidth() - 1, 0);
		g.drawLine(getWidth() - 1, getHeight() - 1, 0, getHeight() - 1);
	}

	public void clearTexture() {
		key.clear();
		list.clear();
	}

	public void addTexture(String url, String name) {
		key.add(name);
		list.put(name, url);
		this.repaint();
	}

	public String getSeletePng() {
		if (select > -1)
			return list.get(key.get(select));
		return null;
	}

	public void setSelect(int num) {
		select = num;
	}

	public void addTexture(String info) {
		boolean asd = false;
		String qw = info.split("=")[0];
		String as = info.split("=")[1];
		for (int i = 0; i < key.size(); i++)
			if (key.get(i).indexOf(qw) > -1) {
				asd = true;
				if (as.equals("0")) {
					key.remove(i);
				} else {
					key.remove(i);
					key.add(info);
				}
			}
		if (!asd && !as.equals("0")) {
			key.add(info);
		}
		this.repaint();
	}

	public int[][] getBlockCoorArray(int[] coor) {
		int[][] a = new int[coor[2] - coor[0] + 1][coor[3] - coor[1] + 1];
		for (int i = 0; i < key.size(); i++) {
			int x = Integer.valueOf(key.get(i).split(",")[0]);
			int y = Integer.valueOf(key.get(i).split(",")[1].split("=")[0]);
			int num = Integer.valueOf(key.get(i).split("=")[1]);
			a[x - coor[0]][y - coor[1]] = num;
		}
		return a;
	}
}
